package com.huitone.gddw.controller.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.huitone.gddw.controller.common.CommonController;
import com.huitone.gddw.dao.entity.Dept;
import com.huitone.gddw.dao.entity.DeptSrvNode;
import com.huitone.gddw.dao.entity.SmLog;
import com.huitone.gddw.dao.entity.UserDept;
import com.huitone.gddw.model.support.Message;
import com.huitone.gddw.model.support.QueryPage;
import com.huitone.gddw.service.dto.system.DeptDTO;
import com.huitone.gddw.service.system.IDeptService;
import com.huitone.gddw.service.system.IDeptSrvNodeService;
import com.huitone.gddw.service.system.IUserDeptService;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 组织单位管理 控制器
 * @author wuluodan
 *
 */
@Controller
@RequestMapping(value="dept")
public class DeptController extends CommonController {

	private final static Logger LOGGER = Logger.getLogger(DeptController.class);
	
	@Autowired
	private IDeptService deptService;
	
	@Autowired
	private IUserDeptService userDeptService;
	
	@Autowired
	private IDeptSrvNodeService deptSrvNodeService;
	
	/**
	 * 查询
	 * @param page
	 * @param request
	 * @return QueryPage
	 */
	@ResponseBody
	@RequestMapping(value="query")
	public QueryPage<DeptDTO> query(QueryPage<Dept> page, HttpServletRequest request) {
		super.logType = SmLog.LOGTYPE_SELECT;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]查询单位管理列表";
		QueryPage<DeptDTO> result = null;
		try {
			result = deptService.selectPage(page);
		} catch (Exception e) {
			LOGGER.error("查询组织单位管理信息出现异常：" + e.getMessage(), e);
			super.logResult = SmLog.RESULT_FAIL;
		}
		super.saveSmLog(super.logType, super.logContent, super.logResult);
		return result;
	}
	
	/**
	 * 获取所有组织单位
	 * @return List
	 */
	@ResponseBody
	@RequestMapping(value="getAll")
	public List<Dept> getAll() {
		try {
			List<Dept> list = deptService.getAll();
			return list;
		} catch (Exception e) {
			LOGGER.error("获取所有组织单位信息过程中出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 根据单位水平获取所有上一级单位
	 * @param deptLevel 单位水平
	 * @return List
	 */
	@ResponseBody
	@RequestMapping(value="getUpLevelDept")
	public List<Dept> getUpLevelDept(String deptLevel) {
		try {
			List<Dept> list = deptService.getUpLevelDept(deptLevel);
			return list;
		} catch (Exception e) {
			LOGGER.error("获取所有上一级单位信息过程中出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 获取单位所有的业务端
	 * @param deptId 单位ID
	 * @return List
	 */
	@ResponseBody
	@RequestMapping(value="getDeptSvrNodes")
	public List<DeptSrvNode> getDeptSvrNodes(String deptId) {
		try {
			List<DeptSrvNode> list = deptSrvNodeService.getByDeptId(deptId);
			return list;
		} catch (Exception e) {
			LOGGER.error("获取单位所有的业务端信息过程中出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 修改页面数据准备
	 * @param deptId 单位ID
	 * @return DeptDTO
	 */
	@ResponseBody
	@RequestMapping(value="toUpdate")
	public DeptDTO toUpdate(String deptId) {
		try {
			DeptDTO entity = deptService.toUpdate(deptId);
			return entity;
		} catch (Exception e) {
			LOGGER.error("跳转修改页面过程中出现异常：" + e.getMessage(), e);
			return null;
		}
	}
	
	/**
	 * 保存
	 * @param deptDTO DeptDTO
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="save")
	public Message save(DeptDTO deptDTO) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]添加单位信息";
		try {
			List<Dept> list = deptService.getByName(deptDTO.getDeptName());
			if (list != null && list.size() > 0) {
				super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
				return new Message(false, "该单位已存在，不能重复添加！");
			}
			deptService.save(deptDTO);
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "保存成功！");
		} catch (Exception e) {
			LOGGER.error("保存组织单位管理信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "保存失败！");
		}
	}
	
	/**
	 * 更新
	 * @param deptDTO DeptDTO
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="update")
	public Message update(DeptDTO deptDTO) {
		super.logType = SmLog.LOGTYPE_UPDATE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]修改单位信息";
		try {
			Dept entity = deptService.getById(deptDTO.getDeptId());
			if (!deptDTO.getDeptName().equals(entity.getDeptName())) {
				List<Dept> list = deptService.getByName(deptDTO.getDeptName());
				if (list != null && list.size() > 0) {
					super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
					return new Message(false, "该单位已存在，不能重复添加！");
				}
			}
			deptService.update(deptDTO);
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "修改成功！");
		} catch (Exception e) {
			LOGGER.error("修改组织单位管理信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "修改失败！");
		}
	}
	
	/**
	 * 删除
	 * @param deptId 单位ID
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="delete")
	public Message delete(String deptId) {
		super.logType = SmLog.LOGTYPE_DELETE;
		super.logContent = "[" + SysLoginUtil.getLoginUserId() + "]删除单位信息";
		try {
			List<Dept> listParentDept = deptService.getByParentId(deptId);
			if (!listParentDept.isEmpty() || listParentDept.size() != 0) {
				super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
				return new Message(false, "该单位已有下级单位，无法删除！");
			}
			List<UserDept> list = userDeptService.getByDeptId(deptId);
			if (!list.isEmpty() || list.size() != 0) {
				super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
				return new Message(false, "该单位已被用户引用，无法删除！");
			}
			deptService.delete(deptId);
			super.saveSmLog(super.logType, super.logContent, super.logResult);
			return new Message(true, "删除成功！");
		} catch (Exception e) {
			LOGGER.error("删除组织单位管理信息过程中出现异常：" + e.getMessage(), e);
			super.saveSmLog(super.logType, super.logContent, SmLog.RESULT_FAIL);
			return new Message(false, "删除失败！");
		}
	}
	
	/**
	 * 设置业务端
	 * @param deptId 单位ID
	 * @param svrNodes 多个业务端编码
	 * @return Message
	 */
	@ResponseBody
	@RequestMapping(value="changeDeptSvrNodes")
	public Message changeDeptSvrNodes(String deptId, String svrNodes) {
		try {
			deptService.changeDeptSvrNodes(deptId, svrNodes);
			return new Message(true, "设置业务端成功！");
		} catch (Exception e) {
			LOGGER.error("设置业务端过程中出现异常：" + e.getMessage(), e);
			return new Message(false, "设置业务端失败！");
		}
	}
	
}
